<template>
  <div class="registerCont">
    <ul style="width: 460px;margin-top: 100px">

      <el-form :model="userInfo" ref="userInfo" label-width="100px" class="demo-ruleForm">
        <el-form-item
            label="用户名"
            prop="userName"
            :rules="[{ required: true, message: '用户名不能为空'}]"
        >
          <el-input type="userName" v-model="userInfo.userName" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item
            label="用户名"
            prop="password"
            :rules="[{ required: true, message: '密码不能为空'}]"
        >
          <el-input type="password" v-model="userInfo.password" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('userInfo')">登录</el-button>
          <el-button @click="resetForm('userInfo')">重置</el-button>
        </el-form-item>
      </el-form>

    </ul>
  </div>
</template>

<script>
import '../../assets/css/register.css'
export default {
  name: "loginNamePassword",
  data() {
    return {
      userInfo: {
        userName:'',
        password:''
      },
      isValid: true, //表单校验是否成功
    }
  },

  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const that=this
          this.$axios
              .post(`http://localhost:9090/loan/user/login`, this.$qs.stringify(this.userInfo)).then(
              response => {
                if (response.data.success == true){
                  localStorage.setItem('userName',JSON.stringify(this.userInfo.userName))
                  this.$message({
                    type: 'success',
                    message: '登录成功！'
                  })
                  that.$router.push({name: "admin"})
                }else {
                  this.$message({
                    type: 'info',
                    message: '用户名或密码错误！'
                  })
                }

              },
              error => {
                this.$message({
                  type: 'info',
                  message: '用户名或密码错误！'
                })
              }
          )
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }

  },
}
</script>

<style scoped>

</style>
